﻿package Gameplay 
{	
	import Common.UnitsManager;
	import Gameplay.Cells.GridCell;
	import Gameplay.GridContent.ContentAbstract;
	import Gameplay.GridContent.ShipView;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.CapsStyle;
	import flash.display.LineScaleMode;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.geom.Point;
	
	public class ShipsField extends Sprite
	{
		private const CSize:int = Constants.CELLS_COUNT;
		private const CGlowColor:uint = Constants.DEFAULT_GRID_GLOW_COLOR;
		
		private var FGrid:Sprite;
		private var FUnits:Sprite;
		private var FSize:Number;
		private var FCellSize:Number;
		private var FSelector:GridSelector;
		private var FCells:Vector.<Vector.<GridCell>>;
		private var FShips:Vector.<ShipView>;
		private var FCellsGlow:GlowFilter;
		
		public function ShipsField():void
		{
			FGrid = new Sprite();
			FUnits = new Sprite();
			FUnits.mouseEnabled = false;
			FUnits.cacheAsBitmap = true;
			addChild(FGrid);
			addChild(FUnits);
			FSize = new Number();
			FCellSize = new Number();
			FCells = new Vector.<Vector.<GridCell>>(CSize);
			for (var i in FCells)
				FCells[i] = new Vector.<GridCell>(CSize);
			FShips = new Vector.<ShipView>;
			FCellsGlow = new GlowFilter(CGlowColor, 1, 10, 10); 			
			FGrid.filters = [FCellsGlow];
			addEventListener(MouseEvent.MOUSE_DOWN, OnMouseDown);
		}		
		//test
		private function OnMouseDown(e:MouseEvent):void 
		{			
			if (e.target is GridCell)
			{
				var vShip:ShipView = new ShipView(UnitsManager.Units[2]);
				vShip.Field = this;
				vShip.Orientation = ShipDirection.XPlus;
				vShip.MainCell = GridCell(e.target);				
				AddShip(vShip);
			}			
		}
		
		protected function ClearCells():void
		{
			while (FGrid.numChildren > 0)
				FGrid.removeChildAt(0);
		}
		
		public function AddShip(AShip:ShipView):void
		{
			AShip.Update();
			FShips.push(AShip);
			var i:int;
			/*switch (AShip.Orientation) 
			{
				case ShipDirection.XPlus:
				case ShipDirection.XMinus:
				{
					for (i = 0; i < AShip.Size; i++)
						CellAt(AShip.MainCell.Row, AShip.MainCell.Column + i).Color = 0x00FF00;
					break;
				}
				case ShipDirection.ZPlus:
				case ShipDirection.ZMinus:
				{
					for (i = 0; i < AShip.Size; i++)
						CellAt(AShip.MainCell.Row + i, AShip.MainCell.Column).Color = 0x00FF00;
					break;
				}
				default:
				{
					trace('Not ready yet');
				}			
			}
			*/
		}
		
		protected function CellAt(ARow:int, AColumn:int):GridCell
		{
			ARow = Math.min(ARow, CSize - 1);
			AColumn = Math.min(AColumn, CSize - 1);
			return FCells[ARow][AColumn];
		}
		
		public function get Size():Number
		{
			return FSize;
		}
		
		public function set Size(ASize:Number):void
		{
			ClearCells();
			FSize = ASize;
			var vWidth:Number = FSize / CSize;
			var vHeight:Number = vWidth / 2;
			var vRow:int = 0;
			var vColumn:int = 0;
			for (var i:int = 0; i < CSize; i++)
			{				
				for (var j:int = 0; j < CSize; j++)
				{
					var vCell:GridCell = new GridCell();
					vCell.CellWidth = vWidth;
					vCell.CellHeight = vHeight;
					vCell.Row = vRow;
					vCell.Column = vColumn;				
					vCell.Draw();					
					vCell.x = (FSize - vCell.width) / 2 + vCell.width / 2 * vColumn - vCell.height * vRow;
					vCell.y = (vColumn + vRow) * vCell.height / 2;					
					FGrid.addChild(vCell);
					vCell.Assign(FCells[i][j]);
					FCells[i][j] = vCell;
					vColumn++;					
				}
				vRow++;
				vColumn = 0;
			}
			
			FGrid.x = (FGrid.width - FSize) * Math.SQRT1_2;
			
			for (var k:int; k < FShips.length; k++)
			{
				FShips[k].MainCell = CellAt(FShips[k].MainCell.Row, FShips[k].MainCell.Column);
				FShips[k].Update();
			}
		}
		
		public function get CellSize():Number
		{
			return FCellSize;
		}
		
		public function get UnitsLayer():Sprite
		{
			return FUnits;
		}
		
		public function set Selector(ASelector:GridSelector):void
		{
			if (FSelector)
			{
				removeChild(FSelector);
				FSelector = null;
			}
			FSelector = ASelector;
			FSelector.mouseEnabled = false;			
			addChild(FSelector);			
		}
	}
}